package org.jay.angla.chart;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.jay.angla.onlab.R;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;

public class WSChart extends AbstractDemoChart {
	private XYMultipleSeriesDataset mDataset;
	private XYMultipleSeriesRenderer mRenderer;

	private XYSeries[] series;
	private XYSeries series2;
	private Context mContext;

	public WSChart(Context context) {
		super();
		series = new XYSeries[20];

		mDataset = new XYMultipleSeriesDataset();
		mRenderer = new XYMultipleSeriesRenderer();

		int[] colors = new int[] { Color.BLUE, Color.CYAN, Color.DKGRAY,
				Color.GRAY, Color.GREEN, Color.LTGRAY, Color.MAGENTA,
				Color.RED, Color.WHITE, Color.YELLOW, Color.BLUE, Color.CYAN,
				Color.DKGRAY, Color.GRAY, Color.GREEN, Color.LTGRAY,
				Color.MAGENTA, Color.RED, Color.WHITE, Color.YELLOW };
		PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE,
				PointStyle.CIRCLE, PointStyle.CIRCLE, PointStyle.CIRCLE,
				PointStyle.CIRCLE, PointStyle.CIRCLE, PointStyle.CIRCLE,
				PointStyle.CIRCLE, PointStyle.CIRCLE, PointStyle.CIRCLE,
				PointStyle.CIRCLE, PointStyle.CIRCLE, PointStyle.CIRCLE,
				PointStyle.CIRCLE, PointStyle.CIRCLE, PointStyle.CIRCLE,
				PointStyle.CIRCLE, PointStyle.CIRCLE, PointStyle.CIRCLE,
				PointStyle.CIRCLE };
		mRenderer = buildRenderer(colors, styles);
		for (int i = 0; i < 20; i++) {
			((XYSeriesRenderer) mRenderer.getSeriesRendererAt(i))
					.setFillPoints(true);
			((XYSeriesRenderer) mRenderer.getSeriesRendererAt(i))
					.setLineWidth(4.0f);
		}
		mRenderer.setApplyBackgroundColor(true);
		mRenderer.setMarginsColor(Color.argb(0, 0xff, 0xff, 0xff));
		mRenderer.setChartTitleTextSize(30);
		mRenderer.setAxisTitleTextSize(25);
		mRenderer.setMargins(new int[] { 50, 80, 50, 50 });
		mRenderer.setXLabelsColor(Color.WHITE);
		mRenderer.setYLabelsColor(0, Color.WHITE);

		setChartSettings(mRenderer, "",
				context.getString(R.string.wavelength_chart_title),
				context.getString(R.string.abs_chart_title), 350.0f, 1100.0f,
				0.00f, 3.00f, Color.WHITE, Color.WHITE);
		mRenderer.setXLabels(10);
		mRenderer.setYLabels(8);
		mRenderer.setShowGrid(true);
		mRenderer.setXLabelsAlign(Align.RIGHT);
		mRenderer.setYLabelsAlign(Align.RIGHT);
		mRenderer.setYLabelsPadding(10);

		mRenderer.setZoomEnabled(false, false);

		mRenderer.setZoomEnabled(false);
		mRenderer.setClickEnabled(false);
		mRenderer.setPanEnabled(true, true);
		mRenderer.setLabelsTextSize(30);
		mRenderer.setPointSize(0.0f);
		mRenderer.setShowLegend(false);

		for (int i = 0; i < 19; i++) {
			series[i] = new XYSeries("test " + i);
		}
		series2 = new XYSeries("test2");
	}

	public void setAbsTitle() {
		mRenderer.setYTitle(mContext.getString(R.string.abs_chart_title));
	}

	public void setEnergyTitle() {
		mRenderer.setYTitle(mContext.getString(R.string.energy_string));
	}

	public void setTransTitle() {
		mRenderer.setYTitle(mContext.getString(R.string.trans_chart_title));
	}

	public void setXLimit(float start, float end) {
		mRenderer.setXAxisMin(start);
		mRenderer.setXAxisMax(end);
	}

	public void setYLimit(float start, float end) {
		mRenderer.setYAxisMin(start);
		mRenderer.setYAxisMax(end);
	}

	public void setYTitle(String title) {
		mRenderer.setYTitle(title);
	}

	public GraphicalView getChartView(Context context) {
		mContext = context;
		for (int i = 0; i < 19; i++) {
//			series[i].add(0, 0);
			mDataset.addSeries(series[i]);
		}
		mDataset.addSeries(series2);
		return ChartFactory.getLineChartView(context, mDataset, mRenderer);
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "WS Chart";
	}

	@Override
	public String getDesc() {
		// TODO Auto-generated method stub
		return "By JayZhang";
	}

	public void add(int index, float x, float y) {
		series[index].add(x, y);
	}

	public void add2(float x, float y) {
		series2.add(x, y);
	}

	public void clear(int index) {
		series[index].clear();
	}

	public void clear2() {
		series2.clear();
	}

	@Override
	public Intent execute(Context context) {
		// TODO Auto-generated method stub
		return null;
	}

}
